package com.qf.ssm.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.CarOrderMapper;
import com.qf.ssm.po.*;
import com.qf.ssm.qo.*;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarOrderService;
import com.qf.ssm.utlis.CommonBeanutils;
import com.qf.ssm.vo.OrderAddStoreVo;
import com.qf.ssm.vo.OrderAddVo;
import com.qf.ssm.vo.OrderListVo;
import com.qf.ssm.vo.OrderCarTypeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import java.util.List;

/**
 * @author 黄梓濠
 * @Create 2024-01-02-20:48
 */
@Service
@Slf4j
public class CarOrderServiceImpl implements CarOrderService {
    @Autowired
    private CarOrderMapper carOrderMapper;
    @Autowired
    private Snowflake snowflake;
    //    根据关键字 分页查询订单列表
    @Override
    public PageInfo<OrderListVo> getKeywordPageing(Integer page, Integer size, OrderListQo orderListQo) {
        //qo转换为po
        CarOrder carOrder = CommonBeanutils.convertTo(orderListQo, CarOrder::new);
        PageInfo<CarOrder> objectPageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> carOrderMapper.selectList(carOrder));
        PageInfo<OrderListVo> carOrderVOPageInfo = new PageInfo<>();
        //将po转换为vo
        PageInfo<OrderListVo> orderListVoPageInfo = CommonBeanutils.convertPageInfo(objectPageInfo, carOrderVOPageInfo, OrderListVo::new);
        System.out.println(orderListVoPageInfo);
        return orderListVoPageInfo;
    }


//    //根据取车门店 还车门店查询门店id
//    @Override
//    public List<Store> selectByDetailLocation(CarOrderQO carOrderQO) {
//        List<Store> stores = null;
//        try {
//            stores = carOrderMapper.selectByDetailLocation(carOrderQO);
//            if (stores.size() !=2){
//                throw new ServiceException(ResultStatus.ADDRESS_ERROR);
//            }
//        } catch (ServiceException e) {
//            throw new ServiceException(ResultStatus.SYS_ERROR);
//        }
//
//        return stores;
//    }

    //    根据承租人 电话 身份证号查询用户id
    @Override
    public Customer selectBykey(OrderAddQo orderAddQo) {
        //qo转换为po
        CarOrder carOrder = CommonBeanutils.convertTo(orderAddQo, CarOrder::new);
        Customer customer = null;
        try {
            customer = carOrderMapper.selectBykey(carOrder);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SYS_ERROR);
        }
        if (ObjectUtils.isEmpty(customer)){
            throw new ServiceException(ResultStatus.CUSTOMER_ERROR);
        }
        return customer;
    }

    //获取添加订单原来有的数据
    @Override
    public OrderAddVo getOrderAdd() {
        List<OrderStatus> orderStatuses = null;
        List<OrderCarTypeVo> orderCarTypeVos = null;
        List<OrderAddStoreVo> orderAddStoreVos = null;
        try {
            orderStatuses = carOrderMapper.selectOrderStatusAll(); //状态id
            orderCarTypeVos = carOrderMapper.selectAllCarType(); //得到carType id
            orderAddStoreVos = carOrderMapper.selectAllStore(); //取车还车门店id
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SYS_ERROR);
        }
        OrderAddVo orderAddVo = new OrderAddVo();
        orderAddVo.setOrderSource(orderStatuses);
        orderAddVo.setOrderCarTypeVo(orderCarTypeVos);
        orderAddVo.setOrderAddStoreVo(orderAddStoreVos);
        return orderAddVo;
    }
    //根据车型  车牌号查询车型id
    @Override
    public CarInfo selectByTypeAndNum(OrderAddQo orderAddQo) {
        CarOrder carOrder = CommonBeanutils.convertTo(orderAddQo, CarOrder::new);
        CarInfo carInfo = null;
        try {
            carInfo = carOrderMapper.selectByTypeAndNum(carOrder);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SYS_ERROR);
        }
        if (ObjectUtils.isEmpty(carInfo)){
            throw new ServiceException(ResultStatus.TYPE_NO_EXIST);
        }
        return carInfo;
    }


    //添加订单 参数传入： 来源id 车型id
    @Override
    public Integer addCarOrder(OrderAddQo orderAddQo) {
        Integer count = null;
        CarOrder carOrder = null;
        //qo转换为po
        carOrder = CommonBeanutils.convertTo(orderAddQo, CarOrder::new);
        //顾客id
        Customer customer = selectBykey(orderAddQo);
        Integer customerId = customer.getId();
        carOrder.setCustomerId(customerId);

        //车型id 车牌 查询carIfo主键
        CarInfo carInfo = selectByTypeAndNum(orderAddQo);
        carOrder.setCarId(carInfo.getCid());
        //订单号(雪花算法生成)
        Snowflake snowflake1 = IdUtil.getSnowflake(1, 1);
        int orderNumber = (int) snowflake1.nextId();
        carOrder.setOrderNumber(orderNumber);

        try {
            count = carOrderMapper.insertCarOrder(carOrder);
            if (count < 1){
                throw new ServiceException(ResultStatus.SQL_INSERT_ERROR);
            }
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SYS_ERROR);
        }

        return count;
    }


    //修改订单
    @Override
    public Integer editCarOrder(OrderAddQo orderAddQo) {
        //qo转换为po
        CarOrder carOrder = null;
        carOrder = CommonBeanutils.convertTo(orderAddQo, CarOrder::new);
        //顾客id
        Customer customer = selectBykey(orderAddQo);
        Integer customerId = customer.getId();
        carOrder.setCustomerId(customerId);

        //车型id 车牌 查询carIfo主键
        CarInfo carInfo = selectByTypeAndNum(orderAddQo);
        carOrder.setCarId(carInfo.getCid());
        Integer count = null;
        try {
            count = carOrderMapper.updateCarOrder(carOrder);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SYS_ERROR);
        }
        if (count < 1){
            throw new ServiceException(ResultStatus.UPDATE_ERROR);
        }
        return count;
    }


    //设置订单状态
    @Override
    public Integer editOrderStatus( OrderStatusQO orderStatusQO) {
        //qo装po
        OrderStatus orderStatus = CommonBeanutils.convertTo(orderStatusQO, OrderStatus::new);
        Integer count = null;
        try {
            count = carOrderMapper.updateOrderStatus(orderStatus);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SYS_ERROR);
        }
        if (count<1){
            throw new ServiceException(ResultStatus.UPDATE_ERROR);
        }
        return count;
    }

    //上传合同
    @Override
    @Transactional
    public Integer addContract(OrderFileQo orderFileQo) {
        Integer count = carOrderMapper.updateFile(orderFileQo);
        if (count < 1){
            throw new ServiceException(ResultStatus.SQL_INSERT_ERROR);
        }
        return count;
    }


    @Override
    public List<OrderStatus> getOrderStatus() {
        List<OrderStatus> orderStatuses = null;
        try {
            orderStatuses = carOrderMapper.selectOrderStatusAll();
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        return orderStatuses;
    }




}
